package com.anlu.base.jdbc.dao;

import com.anlu.base.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao{

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(User user) {
        //创建sql语句
        String sql = "insert into user (`NAME`,age) values(?,?)";
        Object[] args = {user.getName(), user.getAge()};
        int update = jdbcTemplate.update(sql,args);
        logger.info("insert result:{}",update);
    }

    @Override
    public void update(User user) {
        String sql = "update user set name=? where id=?";
        Object[] args = {user.getName(), user.getId()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }

    @Override
    public List<User> getAll() {
        String sql = "SELECT * FROM USER";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);

        List<User> users = new ArrayList<>();
        if(map != null){
            map.forEach((k,v)->{
                logger.info("key = {} value = {}",k,v);
                User user = new User();
                user.setAge((Integer) map.get("age"));
                user.setName((String) map.get("name"));
                users.add(user);
            });
        }
        return users;
    }

    @Override
    public List<User> getAllByBean() {
        String sql = "select * from user";
        List<User> lists = jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
        return lists;
    }




    @Override
    public User selectOne(Integer id) {
        String sql = "select * from user where id = ?";

        Object[] args = {id};
        int[] argTypes = {java.sql.Types.INTEGER};
        Map<String, Object> map = jdbcTemplate.queryForMap(sql, args, argTypes);


        User user = new User();
        user.setAge((Integer) map.get("age"));
        user.setName((String) map.get("name"));
        return user;
    }

    public User selectOneM1(Integer id) {
        String sql = "select * from user where id = ?";



        User user =jdbcTemplate.queryForObject(sql,new Object[]{id},new BeanPropertyRowMapper<User>(User.class));

        return user;
    }

    @Override
    public User selectOneM2(Integer id) {
        String sql = "select * from user where id = ?";
        Object[] args = {id};
        int[] argTypes = {java.sql.Types.INTEGER};

        User user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),new Object[]{id});
        return user;
    }

    @Override
    public User selectOneM3(Integer id) {
        String sql = "select * from user where id = ?";

        Stream<User> user = jdbcTemplate.queryForStream(sql,new BeanPropertyRowMapper<User>(User.class),new Object[]{id});

        user.forEach(user1 -> {
            logger.info("user1:{}",user1.getName());
        });
        return user.findFirst().get();
    }

    @Override
    public User selectOneM4(Integer id) {
        String sql = "select * from user where id = ?";

        return null;
    }

    @Override
    public Object selectProperty(Integer id) {
        String sql = "select ID from user where id=?";
        Object ret = jdbcTemplate.queryForObject(sql, Integer.class, new Object[]{id});

        return ret;
    }


}
